package com.deeper.customview.ui.c2.demo.c02d03;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.deeper.customview.R;

public class ClipView extends View {
    public ClipView(Context context) {
        super(context);
    }


    public ClipView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    public ClipView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Bitmap dogBmp = BitmapFactory.decodeResource(getResources(), R.mipmap.dog);
        //绘制完整照片
        canvas.drawBitmap(dogBmp, 0, 0, null);
        //平移坐标
        canvas.translate(200, 200);
        //定义剪切区
        canvas.clipRect(new Rect(50, 50, 500, 500));
        //再次绘制照片
        canvas.drawBitmap(dogBmp, 0, 0, null);
    }
}
